﻿Public Class SanPhamDA
    Private cn As DatabaseConnect
    Private sql As String

    Public Sub New()
        cn = New DatabaseConnect()
    End Sub

    Public Function InUp(ByVal sp As SanPhamDT, ByVal flag As Boolean)
        Dim ma As Integer = sp.MaLoai
        Dim ten As String = sp.TenLoai
        Dim tg As String = sp.TriGia
        Dim dvt As String = sp.DonViTinh
        '----------------------------------------------------'
        If flag = True Then
            sql = "INSERT INTO SanPham(tenLoai,trigia,donvitinh,tinhtrang) VALUES(N'" + ten + "'," & tg & "," & dvt & ", 1)"
        Else
            sql = "UPDATE SanPham SET tenLoai = N'" + ten + "', trigia = " & tg & ", donvitinh = " & dvt & "  WHERE MaLoai = " & ma & " "
        End If
        Return cn.Execute(sql)
    End Function

    Public Function UpState(ByVal sp As SanPhamDT)
        Dim ma As Integer = sp.MaLoai
        Dim tt As String = sp.TinhTrang
        sql = "UPDATE SanPham SET tinhtrang = N'" + tt + "' WHERE MaLoai = " & ma
        Return cn.Execute(sql)
    End Function

    Public Function Delete(ByVal sp As SanPhamDT)
        Dim id As Integer = sp.MaLoai
        sql = "delete from SanPham where Id = " & id & ""
        Return cn.Execute(sql)
    End Function

    Public Function IsSelling(ByVal ma As Integer) As Boolean
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM SanPham WHERE MaLoai = " & ma)
        If dt.Rows.Count > 0 Then
            Dim tt As Integer = Convert.ToInt32(dt.Rows(0)("tinhtrang"))
            If tt = 1 Then
                Return True
            Else
                Return False
            End If
        End If
    End Function

    Public Function GetData(ByVal dieukien As String) As List(Of SanPhamDT)
        Dim ma, trigia, dvt As Integer
        Dim ten As String
        Dim sp As SanPhamDT
        Dim list As New List(Of SanPhamDT)
        Dim dt As New DataTable
        dt = cn.GetData("SELECT * FROM SanPham" + dieukien)

        For i As Integer = 0 To dt.Rows.Count - 1
            ma = Convert.ToInt32(dt.Rows(i)("MaLoai"))
            ten = dt.Rows(i)("TenLoai").ToString()
            trigia = Convert.ToInt32(dt.Rows(i)("trigia"))
            dvt = Convert.ToInt32(dt.Rows(i)("donvitinh"))
            '-----'
            sp = New SanPhamDT(ma, ten, trigia, dvt)
            list.Add(sp)
        Next
        Return list
    End Function

    Function GetID() As Integer
        Dim dt As New DataTable
        Dim id As Integer
        dt = cn.GetData("SELECT * FROM SanPham")
        If dt.Rows.Count > 0 Then
            id = Convert.ToInt32(dt.Rows(dt.Rows.Count - 1).Item("MaLoai"))
        Else
            id = 1
        End If
        Return id
    End Function

    Public Function getTriGia(ByVal maloai As Integer) As Double
        Dim trigia As Double
        Dim dt As New DataTable
        dt = cn.GetData("SELECT tg.trigia FROM SanPham sp, SanPham_TriGia tg WHERE sp.trigia = tg.trigiaID AND MaLoai = " & maloai)
        trigia = Convert.ToDouble(dt.Rows(0)("trigia"))
        Return trigia
    End Function
End Class
